package org.eclipse.milo.examples.server.methods;

import org.eclipse.milo.examples.server.ExampleNamespace;
import org.eclipse.milo.opcua.sdk.core.ValueRanks;
import org.eclipse.milo.opcua.sdk.server.api.methods.AbstractMethodInvocationHandler;
import org.eclipse.milo.opcua.sdk.server.nodes.UaMethodNode;
import org.eclipse.milo.opcua.stack.core.Identifiers;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.structured.Argument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ModifyTimeMethod extends AbstractMethodInvocationHandler{
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private ExampleNamespace namespace = null;

    public static final Argument X = new Argument(
            "x",
            Identifiers.Double,
            ValueRanks.Scalar,
            null,
            new LocalizedText("The argument to the modify time root function.")
    );

    public static final Argument X_SQRT = new Argument(
            "x_sqrt",
            Identifiers.Double,
            ValueRanks.Scalar,
            null,
            new LocalizedText("The argument to the modify time root function.")
    );

    public ModifyTimeMethod(UaMethodNode node, ExampleNamespace namespace) {
        super(node);
        this.namespace = namespace;
    }

    @Override
    public Argument[] getInputArguments() {
        return new Argument[]{X};
    }

    @Override
    public Argument[] getOutputArguments() {
        return new Argument[]{X_SQRT};
    }

    @Override
    protected Variant[] invoke(InvocationContext invocationContext, Variant[] inputValues) {
        double x = (double) inputValues[0].getValue();
        this.namespace.SamplingInterval = x;
//        this.namespace.setNodeIntervalTime(x);
        return new Variant[]{new Variant(0)};
    }
}
